//
//  JKStatusPictureView.swift
//  新浪微博
//
//  Created by Jack.Lin on 16/7/6.
//  Copyright © 2016年 Jack.Lin. All rights reserved.
//

import UIKit

class JKStatusPictureView: UIView {

    var viewModel: JKStatusViewModel? {
    
        didSet {
           
            calcViewSize()
            
            // 设置配图视图的 URL 数据
            urls = viewModel?.picURLs
            
        }
    
    }

    
    // 根据视图模型的配图视图大小, 调整显示内容
    private func calcViewSize() {
     
        // 1> 单图, 根据配图视图的大小, 修改 subviews[0] 的宽高
        
        if viewModel?.picURLs?.count == 1 {
            
            let viewSize = viewModel?.pictureViewSize ?? CGSize()
            
            // a) 获取第0个图像视图
            let v = subviews[0]
            v.frame = CGRect(x: 0,
                             y: JKStatusPictureViewOutterMargin,
                             width: viewSize.width,
                             height: viewSize.height - JKStatusPictureViewOutterMargin)
        } else {
            // 2> 多图(无图)，回复 subview[0] 的宽高，保证九宫格布局的完整
            
            let v = subviews[0]
            v.frame = CGRect(x: 0,
                             y: JKStatusPictureViewOutterMargin,
                             width: JKStatusPictureItemWidth,
                             height: JKStatusPictureItemWidth)
        }
        
        // 修改高度约束
        higthConS.constant = viewModel?.pictureViewSize.height ?? 0


    }
    
    
    
    
    /// 配图视图的数组
    private var urls: [JKStatusPicture]? {
        didSet {
            
            // 1. 隐藏所有的 imageView
            for v in subviews {
                v.isHidden = true
            }
            
            // 2. 遍历 urls 数组，顺序设置图像
            var index = 0
            for url in urls ?? [] {
                
                // 获得对应索引的 imageView
                let iv = subviews[index] as! UIImageView
                
                // 4 张图像处理
                if index == 1 && urls?.count == 4 {
                    index += 1
                }

                
                // 设置图像
                iv.cz_setIamge(urlString: url.thumbnail_pic, placeholderImage: nil)
                
                // 显示图像
                iv.isHidden = false
                
                index += 1
            }
        }
    }
    
    
    
    
    @IBOutlet weak var higthConS: NSLayoutConstraint!
    
    override func awakeFromNib() {
        setupUI()
    }
    
}


extension JKStatusPictureView {
    
    // 1. Cell 中所有的控件都是提前准备好
    // 2. 设置的时候，根据数据决定是否显示
    // 3. 不要动态创建控件
    private func setupUI() {
        
        
        backgroundColor = superview?.backgroundColor
        // 超出边界的内容不显示
        clipsToBounds = true
        
        let count = 3
        let rect = CGRect(x: 0,
                          y: JKStatusPictureViewOutterMargin,
                          width: JKStatusPictureItemWidth,
                          height: JKStatusPictureItemWidth)
        
        // 循环创建 9 个 imageView
        for i in 0..<count * count {
            
            let iv = UIImageView()
            
            iv.contentMode = .scaleAspectFill
            iv.clipsToBounds = true
            
            // 行 -> Y
            let row = CGFloat(i / count)
            // 列 -> X
            let col = CGFloat(i % count)
            
            let xOffset = col * (JKStatusPictureItemWidth + JKStatusPictureViewInnerMargin)
            let yOffset = row * (JKStatusPictureItemWidth + JKStatusPictureViewInnerMargin)
            
            iv.frame = rect.offsetBy(dx: xOffset, dy: yOffset)
            
            addSubview(iv)
        }
    }
}
